package entities.enemies;

import it.randomtower.engine.entity.Entity;

import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Vector2f;

import utils.ResourceManager;
import entities.ammo.Rocket;
import entities.base.AbstractEnemy;

public class BazookaClown extends AbstractEnemy {

	private int frame = 0;
	private int frameTimer = 0;
	private int frameInterval = 200;

	private int curAimTime = 0;
	private int aimTime = 1000;

	private int curReloadTime = 0;
	private int reloadTime = 1000;

	private Image idle = null;

	private boolean shooting = false;
	private boolean recentlyShot = false;

	public BazookaClown(float x, float y) {
		super(x, y);

		setMaxHealth(30);

		sheet = ResourceManager.getSpriteSheet("bazookaClown");

		idle = sheet.getSprite(5, 0);
		currentImage = idle;

		gravity = 0.4f;
		maxSpeed = new Vector2f(1f, 10f);
		friction = new Vector2f(0.5f, 0.5f);

		setHitBox(0, 0, currentImage.getWidth(), currentImage.getHeight());
	}

	/**
	 * Feuert eine Rakete ab, sofern ein Spieler in Sicht in.
	 * 
	 * @param delta
	 *            - ms seit letztem Update
	 */
	private void shoot(int delta) {
		shooting = true;
		if (shooting) {
			if (!recentlyShot) {
				curAimTime += delta;
				if (curAimTime >= aimTime) {
					curAimTime = 0;

					if (faceRight)
						world.add(new Rocket(x + currentImage.getWidth() + 5, y + 16, true, this));
					else
						world.add(new Rocket(x - 20, y + 16, false, this));

					recentlyShot = true;
					if (faceRight)
						currentImage = sheet.getSprite(4, 0);
					else
						currentImage = sheet.getSprite(4, 0).getFlippedCopy(true, false);
				}
			} else {
				curReloadTime += delta;
				if (curReloadTime >= reloadTime) {
					curReloadTime = 0;
					recentlyShot = false;
				}
			}

			return;
		}

		if (faceRight)
			currentImage = sheet.getSprite(3, 0);
		else
			currentImage = sheet.getSprite(3, 0).getFlippedCopy(true, false);
	}

	@Override
	public void updateAI(int delta) {
		if (shooting) {
			speed.x = 0;
			acceleration.x = 0;
		}

		if (autoWalk) {
			if (faceRight)
				acceleration.x = 1;
			else
				acceleration.x = -1;
		}

		detectNextStep();

		gravity(delta);
		maxspeed(true, true);

		if (speed.x != 0) {
			frameTimer -= delta;
			while (frameTimer < 0) {
				frame++;
				frameTimer += frameInterval;
				if (shooting) {
					if (frame > 5) {
						frame = 3;
					}
				} else {
					if (frame > 2) {
						frame = 0;
					}
				}
			}
		}

		if (speed.x < 0) {
			currentImage = sheet.getSprite(frame, 0).getFlippedCopy(true, false);
		}
		if (speed.x > 0) {
			currentImage = sheet.getSprite(frame, 0);
		}
		if (speed.x == 0) {
			if (!recentlyShot) {
				if (faceRight) {
					currentImage = idle;
				} else {
					currentImage = idle.getFlippedCopy(true, false);
				}
			}
		}

		if (faceRight) {
			if (collide(Entity.PLAYER, x + 250, y) != null) {
				shoot(delta);
			} else
				shooting = false;
		} else {
			if (collide(Entity.PLAYER, x - 250, y) != null) {
				shoot(delta);
			} else
				shooting = false;
		}
	}

}
